import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class createByClickDialog extends JDialog{
	
	// GUI
	private backgroundPanel panel;
	private JLabel nameLabel;
	private JTextField nameTextField;
	private JLabel phoneLabel;
	private JTextField phoneTextField;
	private JLabel ageLabel;
	private JTextField ageTextField;
	private JLabel genderLabel;
	private ButtonGroup genderGroup;
	private JRadioButton maleRadiobutton;
	private JRadioButton femaleRadiobutton;
	private JComboBox idComboBox;
	private JTextField idTextField;
	private JLabel totalTimeLabel;
	private JTextField hourTextField;
	private JLabel hourLabel;
	private JTextField minTextField;
	private JLabel minLabel;
	private JLabel fromLabel;
	private Calendar startTime;
	private JLabel startTimeLabel;
	private JLabel amountLabel;
	private MyComboBoxModel amountModel;
	private JComboBox amountComboBox;
	private JLabel roomLabel;
	private MyComboBoxModel roomModel;
	private JComboBox roomComboBox;
	private JButton createButton;
	private JButton cancelButton;
	
	// event handles
	private GenderHandler genderHandler;
	private IdTypeHandler idTypeHandler;
	private CreateHandler CreateHandler;
	private CancelHandler CancelHandler;
	
	// data control
	private roomManager rm;
	private host host;
	private int roomNum;
	
	public createByClickDialog(roomManager initRm, int initRoomNum){
		roomNum = initRoomNum;
		rm = initRm;
		host = new host("", -1, -1, -1, "", "", "","",0);
		layoutGUI();
	}

	private void layoutGUI() {
		// TODO Auto-generated method stub
		this.setTitle("Create Room");
		this.setResizable(false);
		this.setSize(600, 300);
		panel = new backgroundPanel("background4.jpg",0,0,600,300);
		panel.setBackground(Color.WHITE);
		nameLabel = new JLabel("Name: ");
		nameLabel.setForeground(Color.WHITE);
		nameTextField = new JTextField(20);

		phoneLabel = new JLabel("Phone: ");
		phoneLabel.setForeground(Color.WHITE);
		phoneTextField = new JTextField(20);
		
		ageLabel = new JLabel("Age: ");
		ageLabel.setForeground(Color.WHITE);
		ageTextField = new JTextField(10);
		
		genderLabel = new JLabel("Gender: ");
		genderLabel.setForeground(Color.WHITE);
		genderGroup = new ButtonGroup();
		maleRadiobutton = new JRadioButton("Male ");
		femaleRadiobutton = new JRadioButton("Female ");
		genderGroup.add(maleRadiobutton);
		genderGroup.add(femaleRadiobutton);
		
		idComboBox = new JComboBox();
		String[] idType = {"ID Type","Drive License","Passport"};
		DefaultComboBoxModel idComboBoxModel = new DefaultComboBoxModel(idType);
		idComboBox.setModel(idComboBoxModel);
		idTextField = new JTextField(20);
		idTextField.setEnabled(false);
		
		totalTimeLabel = new JLabel("Total time: ");
		totalTimeLabel.setForeground(Color.WHITE);
		hourTextField = new JTextField(10);
		hourLabel = new JLabel("h ");
		hourLabel.setForeground(Color.WHITE);
		minTextField = new JTextField(10);
		minLabel = new JLabel("min ");
		minLabel.setForeground(Color.WHITE);
		
		fromLabel = new JLabel("From ");
		fromLabel.setForeground(Color.WHITE);
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		startTime = Calendar.getInstance();
		startTimeLabel = new JLabel(dateFormat.format(startTime.getTime()));
		startTimeLabel.setForeground(Color.WHITE);
		
		amountLabel = new JLabel("People Amount: ");
		amountLabel.setForeground(Color.WHITE);
		amountComboBox = new JComboBox();
		amountComboBox.addItem(rm.typeOfSelectedRoom(roomNum));
		amountComboBox.setEnabled(false);
		roomLabel = new JLabel("Room: ");
		roomLabel.setForeground(Color.WHITE);
		roomComboBox = new JComboBox();
		roomComboBox.addItem(roomNum);
		roomComboBox.setEnabled(false);
		
		createButton = new JButton("Create");
		cancelButton = new JButton("Cancel");
		
		// add handlers
		genderHandler = new GenderHandler();
		idTypeHandler = new IdTypeHandler();
		CreateHandler = new CreateHandler();
		CancelHandler = new CancelHandler();
		
		maleRadiobutton.addActionListener(genderHandler);
		femaleRadiobutton.addActionListener(genderHandler);
		idComboBox.addActionListener(idTypeHandler);
		createButton.addActionListener(CreateHandler);
		cancelButton.addActionListener(CancelHandler);
		
		GridBagLayout layout = new GridBagLayout();
		panel.setLayout(layout);
		addComponent(nameLabel,			panel,	0,	0,	1,	1);
		addComponent(nameTextField,		panel,	1,	0,	4,	1);
		addComponent(phoneLabel,		panel,	0,	1,	1,	1);
		addComponent(phoneTextField,	panel,	1,	1,	4,	1);
		addComponent(ageLabel,			panel,	0,	2,	1,	1);
		addComponent(ageTextField,		panel,	1,	2,	1,	1);
		addComponent(genderLabel,		panel,	2,	2,	1,	1);
		addComponent(maleRadiobutton,	panel,	3,	2,	1,	1);
		addComponent(femaleRadiobutton,	panel,	4,	2,	1,	1);
		addComponent(idComboBox,		panel,	0,	3,	1,	1);
		addComponent(idTextField,		panel,	1,	3,	4,	1);
		addComponent(totalTimeLabel,	panel,	0,	4,	1,	1);
		addComponent(hourTextField,		panel,	1,	4,	1,	1);
		addComponent(hourLabel,			panel,	2,	4,	1,	1);
		addComponent(minTextField,		panel,	3,	4,	1,	1);
		addComponent(minLabel,			panel,	4,	4,	1,	1);	
		addComponent(fromLabel,			panel,	0,	5,	1,	1);
		addComponent(startTimeLabel,	panel,	1,	5,	2,	1);
		addComponent(amountLabel,		panel,	0,	6,	1,	1);
		addComponent(amountComboBox,	panel,	1,	6,	2,	1);
		addComponent(roomLabel,			panel,	2,	6,	1,	1);
		addComponent(roomComboBox,		panel,	3,	6,	2,	1);
		addComponent(createButton,		panel,	3,	7,	1,	1);
		addComponent(cancelButton,		panel,	4,	7,	1,	1);
		
		// size and locate the dialog at the center of the screen
		this.add(panel);
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
	}
	
	// method helping GridBagLayout
	public void addComponent(	JComponent jc, Container c, 
			int x, int y, int w, int h){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	
	class GenderHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			String label = ((AbstractButton) ae.getSource()).getText();
			host.setGender(label);
		}
		
	}
	
	class IdTypeHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if (((JComboBox) arg0.getSource()).getSelectedIndex() == 0)
				idTextField.setEnabled(false);
			else idTextField.setEnabled(true);
		}
		
	}
	
	
	class CreateHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			host.setName(nameTextField.getText());
			host.setIdType((String) idComboBox.getSelectedItem());
			host.setId(idTextField.getText());
			host.setStartTime(startTime);
			host.setAmount((String) rm.typeOfSelectedRoom(roomNum));
			host.setRoomNum(roomNum);
			try{
				host.setPhone(Integer.parseInt(phoneTextField.getText()));
				host.setAge(Integer.parseInt(ageTextField.getText()));
				host.setTotalMin(Integer.parseInt(hourTextField.getText())*60
						+ Integer.parseInt(minTextField.getText()));
				host.setEndTime(startTime, host.getTotalMin());
				
				System.out.println("Create Room: \n"+host.toString());
				// update
				rm.addCurrentHost(host);
				rm.updateRoomState(host.getRoomNum(), 1);
				createByClickDialog.this.setVisible(false);
					
	
			}
			catch(NumberFormatException nfe){
				JOptionPane.showMessageDialog(createByClickDialog.this, 
						"Invalid Input", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}
	
	class CancelHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			createByClickDialog.this.setVisible(false);
		}
		
	}
	
}
